package com.itheima.controller;

import com.itheima.R;
import com.itheima.dto.ArticleDTO;
import com.itheima.dto.ArticlePageDTO;
import com.itheima.entity.Article;
import com.itheima.entity.YddArticle;
import com.itheima.service.YddArticleService;
import com.itheima.vo.PageInfoVO;
import com.itheima.vo.YddArticleVO2;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ydd_article")
@RequiredArgsConstructor
public class ArticleController {
    private final YddArticleService yddArticleService;

    /**
     * 文章列表-分页
     *
     * @return {@link R}
     */
    @GetMapping("/list")
    public R list(ArticlePageDTO articlePageDTO){
        PageInfoVO<YddArticleVO2> pageInfoVO = yddArticleService.list(articlePageDTO);
        return R.success(pageInfoVO);
    }

    /**
     * 添加
     *
     * @param article
     * @return {@link R}
     */
    @PostMapping("/add")
    public R save(@RequestBody Article article){
        yddArticleService.save(article);
        return R.success();
    }

    /**
     * 回显
     *
     * @param id
     * @return {@link R}
     */
    @GetMapping("/rowInfo")
    public R<YddArticleVO2> rowInfo(Long id){
        YddArticleVO2 yddArticleVO2 = yddArticleService.rowInfo(id);
        return R.success(yddArticleVO2);
    }

    /**
     * 文章修改
     *
     * @return
     */
    @PostMapping("/update")
    public R<?> update(@RequestBody ArticleDTO articleDTO){
        yddArticleService.update(articleDTO);
        return R.success();
    }

    /**
     * 删除文章
     *
     * @param id
     * @return {@link R}
     */
    @GetMapping("/del")
    public R delete(Long id){
        yddArticleService.delete(id);
        return R.success();
    }


}
